<template>
  <div>
    <div class="head">
      <div class="flex">
        <el-button @click="back" icon="el-icon-arrow-left">返回</el-button>
        <span style="margin-left: 10px">课程列表</span>
      </div>
      <div class="input">
        <span>课程名称：</span>
        <el-input v-model="queryData.course_name" placeholder="请输入" />
      </div>
      <div class="input">
        <span>培训类型：</span>
        <el-cascader
          :show-all-levels="false"
          :options="options2"
          v-model="queryData.selected_type_ids"
          :props="{ multiple: true, checkStrictly: true, label: 'title', value: 'id' }"
          clearable
        ></el-cascader>
      </div>
      <el-button type="primary" class="btn" @click="search">查询</el-button>
      <el-button type="primary" class="btn" @click="reset">重置</el-button>
    </div>
    <div class="box">
      <el-table
        :data="tableData"
        border
        v-loading="isTableLoading"
        empty-text="暂无数据"
        no-filtered-userFrom-text="暂无筛选结果"
        style="width: 100%"
      >
        <el-table-column fixed type="index" align="center" label="序号" width="50" />
        <el-table-column
          fixed
          prop="course_name"
          align="center"
          show-overflow-tooltip
          label="课程名称"
          min-width="140"
        />
        <el-table-column prop="ware_duration" align="center" label="课时(分)" min-width="130" />
        <el-table-column prop="ware_count" align="center" label="课件数量" min-width="100"></el-table-column>
        <el-table-column prop="type_name" align="center" label="培训类型" min-width="140"></el-table-column>
        <el-table-column prop="create_time" align="center" label="创建时间" min-width="140"></el-table-column>
        <el-table-column fixed="right" align="center" prop="action" label="操作" width="160">
          <template #default="scope">
            <div class="action">
              <span @click="showCourseware(scope.row.id)">查看课件</span>
            </div>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        style="margin-top: 5px"
        @size-change="search"
        @current-change="getList"
        :current-page.sync="queryData.page"
        :page-size.sync="queryData.size"
        layout="total, prev, pager, next,sizes,jumper"
        :total="total"
      >
      </el-pagination>
    </div>

    <el-dialog
      title="课件列表"
      :visible.sync="isShowCourseware"
      width="1000px"
      :before-close="handleCloseCourseware"
      :close-on-click-modal="false"
    >
      <el-table
        :data="coursewareData"
        v-loading="isTableCoursewareLoading"
        border
        empty-text="暂无数据"
        no-filtered-userFrom-text="暂无筛选结果"
        style="width: 100%"
        max-height="500"
      >
        <el-table-column fixed type="index" align="center" label="序号" width="50" />
        <el-table-column fixed prop="title" align="center" show-overflow-tooltip label="课件名称" min-width="140" />
        <el-table-column prop="type_name" align="center" label="培训类型" min-width="140"></el-table-column>
        <el-table-column prop="teacher_name" align="center" label="讲师姓名" min-width="120"></el-table-column>
        <el-table-column prop="duration" align="center" label="课长" min-width="100">
          <template #default="scope">
            <span>{{ scope.row.duration || 0 }}分钟</span>
          </template>
        </el-table-column>
        <el-table-column prop="hour" align="center" label="学时" min-width="100"/>
        <el-table-column prop="create_time" align="center" label="创建时间" min-width="140"></el-table-column>
      </el-table>
      <span slot="footer" class="dialog-footer">
        <el-button @click="handleCloseCourseware">关闭</el-button>
      </span>
    </el-dialog>

  </div>
</template>

<script>
import { dateFormat } from '../../libs/util';
import { getCourseware, PlanCourses, TrainTypeTree } from "@/api/manage";
export default {
  name: 'cultivate',
  data() {
    return {
      queryData: {
        course_name: '',
        type_ids: [],
        selected_type_ids: [],
        page: 1,
        size: 10,
        id:this.$route.query.id
      },
      total: 0,
      isTableLoading: false,
      tableData: [],
      dialogVisible: false,
      options2: [],
      coursewareData: [],
      isShowCourseware: false,
      isTableCoursewareLoading: false,
    };
  },
  watch: {
    'queryData.selected_type_ids'(newValue, oldValue) {
      if (this.queryData.selected_type_ids.length > 0) {
        this.queryData.type_ids = [];
        this.queryData.selected_type_ids.forEach((item) => {
          this.queryData.type_ids.push(item[item.length - 1]);
        });
      } else {
        this.queryData.type_ids = [];
      }
    },
  },
  computed: {},
  created() {},
  mounted() {
    this.getList();
    this.getTrainTypeTree();
  },
  methods: {
    search() {
      this.queryData.page = 1;
      this.getList();
    },
    reset() {
      this.queryData.course_name = '';
      this.queryData.selected_type_ids = [];
      this.search();
    },
    edit(item, type) {
      if (type == 1) {
        this.$router.push({
          path: '/company/addCourse/index',
        });
      } else {
        this.$router.push({
          path: '/company/courseDetails/index',
        });
      }
    },
    del(item) {
      this.dialogVisible = true;
      this.item = item;
    },
    delOk() {
      this.$delete(this.tableData, this.item.$index);
      this.dialogVisible = false;
    },
    add() {
      this.$router.push({
        path: '/company/addCourse/index',
      });
    },
    getList() {
      this.isTableLoading = true;
      this.queryData.id = this.$route.query.id;
      PlanCourses(this.queryData).then((res) => {
        this.tableData = res.data.list;
        this.total = res.data.total;
        this.isTableLoading = false;
      });
    },
    getTrainTypeTree() {
      TrainTypeTree().then((res) => {
        this.options2 = res.data.list;
      });
    },
    back() {
      this.$router.back();
    },
    showCourseware(id) {
      this.isTableCoursewareLoading = true;
      getCourseware({course_id: id}).then((res) => {
        this.isTableCoursewareLoading = false;
        this.coursewareData = res.data;
      });
      this.isShowCourseware = true;
    },
    handleCloseCourseware() {
      this.isShowCourseware = false;
    },
  },
};
</script>

<style scoped>
.head {
  width: 100%;
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  background: #ffffff;
  border-radius: 5px;
  padding: 20px;
  padding-bottom: 0px;
}
.input {
  display: flex;
  align-items: center;
  margin-right: 15px;
  margin-bottom: 20px;
}
.input > span {
  font-size: 12px;
  flex-shrink: 0;
}
.btn {
  margin-bottom: 20px;
}
.box {
  width: 100%;
  background: #ffffff;
  border-radius: 5px;
  padding: 20px;
  margin-top: 20px;
}
.action {
  display: flex;
  align-items: center;
  justify-content: center;
}
.action span {
  color: #0256ff;
  font-size: 12px;
  cursor: pointer;
  margin: 0 8px;
}
.flex {
  width: 100%;
  align-items: center;
  margin-bottom: 20px;
}
</style>
